package com.jd.model;

import com.jd.constant.Constants;
import lombok.Data;

import java.io.Serializable;


@Data
public class Response<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = Constants.SUCCESS;

    public static final String SUCCESS_MESSAGE = Constants.SUCCESS_MESSAGE;



    /**
     * 失败
     */
    public static final int FAIL = Constants.FAIL;

    private int code;

    private String message;

    private T data;

    public static <T> Response<T> ok() {
        return restResponseesult(null, SUCCESS, SUCCESS_MESSAGE);
    }

    public static <T> Response<T> ok(T data) {
        return restResponseesult(data, SUCCESS, SUCCESS_MESSAGE);
    }

    public static <T> Response<T> ok(T data, String message) {
        return restResponseesult(data, SUCCESS, message);
    }

    public static <T> Response<T> fail() {
        return restResponseesult(null, FAIL, null);
    }

    public static <T> Response<T> fail(String message) {
        return restResponseesult(null, FAIL, message);
    }

    public static <T> Response<T> fail(T data) {
        return restResponseesult(data, FAIL, null);
    }

    public static <T> Response<T> fail(T data, String message) {
        return restResponseesult(data, FAIL, message);
    }

    public static <T> Response<T> fail(int code, String message) {
        return restResponseesult(null, code, message);
    }

    private static <T> Response<T> restResponseesult(T data, int code, String message) {
        Response<T> apiResponseesult = new Response<>();
        apiResponseesult.setCode(code);
        apiResponseesult.setData(data);
        apiResponseesult.setmessage(message);
        return apiResponseesult;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getmessage() {
        return message;
    }

    public void setmessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
